<script setup lang="ts">
import { ref, watch, computed } from 'vue'

interface Model {
  id: string
  name: string
  description: string
}

const props = defineProps<{
  models: Model[]
  modelValue: string
}>()

const emit = defineEmits<{
  (e: 'update:modelValue', value: string): void
}>()

const isOpen = ref(false)
const selectedModel = ref(props.modelValue)

// 当选择改变时触发事件
watch(selectedModel, (newValue) => {
  emit('update:modelValue', newValue)
})

// 获取当前选中的模型信息
const currentModel = computed(() => {
  return props.models.find(model => model.id === selectedModel.value) || props.models[0]
})

// 切换下拉菜单
const toggleDropdown = () => {
  isOpen.value = !isOpen.value
}

// 选择模型
const selectModel = (modelId: string) => {
  selectedModel.value = modelId
  isOpen.value = false
}

// 点击外部关闭下拉框
const handleClickOutside = (event: MouseEvent) => {
  const target = event.target as HTMLElement
  const dropdown = document.querySelector('.model-selector') as HTMLElement
  
  if (dropdown && !dropdown.contains(target)) {
    isOpen.value = false
  }
}

// 监听点击事件
watch(isOpen, (newVal) => {
  if (newVal) {
    setTimeout(() => {
      window.addEventListener('click', handleClickOutside)
    }, 0)
  } else {
    window.removeEventListener('click', handleClickOutside)
  }
})
</script>

<template>
  <div class="model-selector">
    <div 
      class="model-selector-toggle" 
      @click.stop="toggleDropdown"
      :class="{ 'active': isOpen }"
    >
      <div class="model-info">
        <span class="model-name">{{ currentModel.name }}</span>
        <span class="model-description">{{ currentModel.description }}</span>
      </div>
      <div class="model-icon">
        <el-icon class="dropdown-icon" :class="{ 'rotated': isOpen }">
          <i class="el-icon-arrow-down"></i>
        </el-icon>
      </div>
    </div>
    
    <transition name="dropdown">
      <div class="model-dropdown" v-show="isOpen">
        <div 
          v-for="model in models" 
          :key="model.id" 
          class="model-option"
          :class="{ 'selected': model.id === selectedModel }"
          @click="selectModel(model.id)"
        >
          <div class="model-option-icon">
            <div class="model-icon-badge" :class="model.id"></div>
          </div>
          <div class="model-option-content">
            <div class="model-option-name">{{ model.name }}</div>
            <div class="model-option-description">{{ model.description }}</div>
          </div>
          <div class="model-option-check" v-if="model.id === selectedModel">
            <el-icon><i class="el-icon-check"></i></el-icon>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<style scoped lang="scss">
.model-selector {
  position: relative;
  user-select: none;
  width: 280px;
  z-index: 10;
  
  .model-selector-toggle {
    display: flex;
    align-items: center;
    padding: 16px 22px;
    border-radius: 16px;
    border: none;
    background: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(10px);
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    box-shadow: 0 4px 16px rgba(99, 102, 241, 0.08), 
                0 0 0 1px rgba(99, 102, 241, 0.06);
    
    &:hover {
      box-shadow: 0 6px 22px rgba(99, 102, 241, 0.12),
                  0 0 0 1px rgba(99, 102, 241, 0.1);
      transform: translateY(-3px);
    }
    
    &.active {
      background: rgba(99, 102, 241, 0.03);
      box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.15), 
                  0 6px 22px rgba(99, 102, 241, 0.12);
    }
    
    .model-info {
      display: flex;
      flex-direction: column;
      flex: 1;
    }
    
    .model-name {
      font-weight: 600;
      font-size: 1rem;
      margin-bottom: 3px;
      color: #374151;
    }
    
    .model-description {
      color: #7c8db5;
      font-size: 0.88rem;
    }
    
    .model-icon {
      margin-left: 12px;
      
      .dropdown-icon {
        color: #7c8db5;
        transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
        
        &.rotated {
          transform: rotate(180deg);
          color: #6366F1;
        }
      }
    }
  }
  
  .model-dropdown {
    position: absolute;
    top: calc(100% + 12px);
    left: 0;
    width: 100%;
    border-radius: 16px;
    background-color: rgba(255, 255, 255, 0.98);
    backdrop-filter: blur(16px);
    box-shadow: 0 10px 30px rgba(99, 102, 241, 0.15),
                0 0 0 1px rgba(99, 102, 241, 0.06);
    z-index: 10;
    overflow: hidden;
    border: none;
    
    &::before {
      content: '';
      position: absolute;
      top: -7px;
      left: 40px;
      width: 14px;
      height: 14px;
      background-color: white;
      transform: rotate(45deg);
      box-shadow: -3px -3px 5px rgba(99, 102, 241, 0.06);
    }
    
    .model-option {
      display: flex;
      align-items: center;
      padding: 18px;
      cursor: pointer;
      transition: all 0.3s;
      position: relative;
      border-bottom: 1px solid rgba(99, 102, 241, 0.06);
      
      &:last-child {
        border-bottom: none;
      }
      
      &:hover {
        background-color: rgba(99, 102, 241, 0.04);
        transform: translateX(3px);
      }
      
      &.selected {
        background: linear-gradient(90deg, rgba(99, 102, 241, 0.08) 0%, rgba(139, 92, 246, 0.08) 100%);
        
        &::after {
          content: '';
          position: absolute;
          left: 0;
          top: 0;
          bottom: 0;
          width: 3px;
          background: linear-gradient(to bottom, #6366F1, #8B5CF6);
          border-radius: 0 4px 4px 0;
        }
      }
      
      .model-option-icon {
        margin-right: 16px;
        
        .model-icon-badge {
          width: 42px;
          height: 42px;
          border-radius: 12px;
          display: flex;
          align-items: center;
          justify-content: center;
          box-shadow: 0 6px 18px rgba(99, 102, 241, 0.15);
          
          &.buding-ai {
            background: linear-gradient(135deg, #6366F1 0%, #8B5CF6 100%);
          }
          
          &.buding-creative {
            background: linear-gradient(135deg, #EC4899 0%, #F97316 100%);
          }
          
          &.buding-code {
            background: linear-gradient(135deg, #10B981 0%, #3B82F6 100%);
          }
        }
      }
      
      .model-option-content {
        flex: 1;
      }
      
      .model-option-name {
        font-weight: 600;
        margin-bottom: 5px;
        color: #374151;
      }
      
      .model-option-description {
        font-size: 0.88rem;
        color: #7c8db5;
      }
      
      .model-option-check {
        color: #6366F1;
        margin-left: 12px;
      }
    }
  }
}

// 动画
.dropdown-enter-active, .dropdown-leave-active {
  transition: opacity 0.4s, transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.dropdown-enter-from, .dropdown-leave-to {
  opacity: 0;
  transform: translateY(-15px) scale(0.98);
}
</style> 